/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import me.zhengjie.modules.ejweixin.domain.NgTeacherReservation;
import me.zhengjie.modules.ejweixin.mapper.NgCaseInfoMapper;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionSiteInfoMapper;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgTeacherReservationService;
import me.zhengjie.modules.ejweixin.domain.vo.NgTeacherReservationQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgTeacherReservationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;
import me.zhengjie.websocket.MsgType;
import me.zhengjie.websocket.SocketMsg;
import me.zhengjie.websocket.WebSocketServer;
/**
* @description 服务实现
* @author cy
* @date 2025-04-25
**/
@Service
@RequiredArgsConstructor
public class NgTeacherReservationServiceImpl extends ServiceImpl<NgTeacherReservationMapper, NgTeacherReservation> implements NgTeacherReservationService {

    private final NgTeacherReservationMapper ngTeacherReservationMapper;

    private final NgCaseInfoMapper ngCaseInfoMapper;



    @Override
    public PageResult<NgTeacherReservation> queryAll(NgTeacherReservationQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngTeacherReservationMapper.findAll(criteria, page));
    }

    @Override
    public List<NgTeacherReservation> queryAll(NgTeacherReservationQueryCriteria criteria){
        return ngTeacherReservationMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgTeacherReservation resources) {
        resources.setCreateTime(DateUtil.getTimeStamp());
        save(resources);
        try {
            ngCaseInfoMapper.updateAppointmentsCount(resources.getTeacherId());
            WebSocketServer.sendInfo(new SocketMsg("你有一条新消息[团队预约]", MsgType.INFO), "appointmentDesign");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override

    @Transactional(rollbackFor = Exception.class)
    public void update(NgTeacherReservation resources) {
        NgTeacherReservation ngTeacherReservation = getById(resources.getId());
        ngTeacherReservation.copy(resources);
        saveOrUpdate(ngTeacherReservation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgTeacherReservation> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgTeacherReservation ngTeacherReservation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("预约的老师团队", ngTeacherReservation.getTeacherId());
            map.put("电话号码", ngTeacherReservation.getTel());
            map.put("所在区县", ngTeacherReservation.getDistrict());
            map.put("房屋面积", ngTeacherReservation.getBuildingArea());
            map.put("状态", ngTeacherReservation.getStatus());
            map.put("创建者", ngTeacherReservation.getCreateBy());
            map.put("更新者", ngTeacherReservation.getUpdateBy());
            map.put("创建日期", ngTeacherReservation.getCreateTime());
            map.put("更新时间", ngTeacherReservation.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}